/**   
 *  Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
* @Title: Runner.java 
* @Package multithread.framework 
* @Description: TODO
* @author dailey  
* @date 2012-10-31
* @version V1.0   
*/
package multithread.framework;

import multithread.framework.threadmethod.IContainer;
import multithread.framework.threadmethod.ParserFactory;
import multithread.framework.threadmethod.RunnerFactory;
import multithread.framework.utility.Utilities;

/**
 * Runner for all thread method container
 * @author dailey
 *
 */
public abstract class Runner {

	public abstract void run();

	public static Runner getMultiThreadRunner(Object testcase) {
		return getMultiThreadRunner(testcase, null);
	}

	public static Runner getMultiThreadRunner(Class<?> caseClz) {
		return getMultiThreadRunner(Utilities.newInstance(caseClz));
	}

	public static Runner getMultiThreadRunner(Class<?> caseClz, ThreadRunListener threadRunListener) {
		return getMultiThreadRunner(Utilities.newInstance(caseClz), threadRunListener);
	}

	/**
	 * create {@link MultiThreadRunner} to execute testcase
	 * @param testcase Object test case instance
	 * @param threadRunListener {@link ThreadRunListener}
	 * @return {@link Runner}
	 */
	public static Runner getMultiThreadRunner(Object testcase, ThreadRunListener threadRunListener) {
		return new MultiThreadRunner(testcase).addThreadRunListener(threadRunListener);
	}

	private final static class MultiThreadRunner extends Runner {
		private ThreadRunListener threadRunListener = null;
		private Object testCase = null;

		private MultiThreadRunner(final Object testCase) {
			this.testCase = testCase;
		}

		public MultiThreadRunner addThreadRunListener(ThreadRunListener threadRunListener) {
			this.threadRunListener = threadRunListener;
			return this;
		}

		/* (non-Javadoc)
		 * @see multithread.framework.Runner#run()
		 */
		@Override
		public void run() {
			if (testCase == null)
				return;
			IContainer container = ParserFactory.createThreadMethodParserByInstance(testCase).parse(testCase);
			RunnerFactory.config(container, testCase, threadRunListener).run();
		}
	}
}
